﻿package app.view.components.sections 
{
	import app.model.RemoteProxy;
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.components.buttons.GlamurBtnBase;
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.utils.setTimeout;
	
	/**
	 * 0 Входная страница
	 * firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class PageMainMBase extends LabeledSectionBase
	{
		private var _image:Image;
		// app.view.components.sections.PageMainMBase
		
		public var expo_Btn:GlamurBtnBase;
		public var child_Btn:GlamurBtnBase;
		public var russia_Btn:GlamurBtnBase;
		private var _btns:/*GlamurBtnBase*/Array;
		private var _btnChe:Sprite;
		
		public function PageMainMBase() 
		{
			super();
		};
		
		override protected function init():void 
		{
			super.init();
			id = SectionType.PAGE_MAIN;
			_btns = [expo_Btn, child_Btn, russia_Btn];
			//trace('init : ' + _btns);
			
			
			
			_btnChe = new Sprite();
			_btnChe.graphics.beginFill(0xff0000,0);
			_btnChe.graphics.drawRect(0, 0, 120, 120);
			_btnChe.graphics.endFill();
			addChild(_btnChe);
			_btnChe.buttonMode = _btnChe.useHandCursor = true;
			
			_btnChe.x = 1680 - 120 - 30;
			_btnChe.y = 1050 - 120 - 30;
		};
		
		private function build():void
		{
			_image = new Image(1680, 1050, Image.RESIZE_NONE, Image.ALIGN_NONE, 0, 0, false, 1, false);
			addChildAt(_image,0);
			////trace('url',RemoteProxy.DOMEN+_data.data.flash[1].flash[1].flash_path);
			if (_data) 
			{
				if (_data.data.flash[1].flash[1].flash_path!=undefined && _data.data.flash[1].flash[1].flash_path!='') 
				{
					_image.loadImage(RemoteProxy.DOMEN+_data.data.flash[1].flash[1].flash_path);
				};
			};
			
			expo_Btn.doubleClickEnabled = true;
			expo_Btn.anim.visible = false;
			child_Btn.doubleClickEnabled = true;
			child_Btn.anim.visible = false;
			russia_Btn.doubleClickEnabled = true;
			russia_Btn.anim.visible = false;
			
			setEvents();
		};
		
		private function setEvents():void
		{
			expo_Btn.addEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			expo_Btn.addEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			expo_Btn.addEventListener(MouseEvent.CLICK, onClickExpo);
			child_Btn.addEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			child_Btn.addEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			child_Btn.addEventListener(MouseEvent.MOUSE_DOWN, onClickChildren);
			russia_Btn.addEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			russia_Btn.addEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			russia_Btn.addEventListener(MouseEvent.MOUSE_DOWN, onClickRussia);
			
			//для чебурашки
			_btnChe.addEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			_btnChe.addEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			_btnChe.addEventListener(MouseEvent.MOUSE_DOWN, onClickBtnChe);
		}
		
		
		private function removeEvents():void
		{
			expo_Btn.removeEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			expo_Btn.removeEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			expo_Btn.removeEventListener(MouseEvent.CLICK, onClickExpo);
			child_Btn.removeEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			child_Btn.removeEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			child_Btn.removeEventListener(MouseEvent.MOUSE_DOWN, onClickChildren);
			russia_Btn.removeEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			russia_Btn.removeEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			russia_Btn.removeEventListener(MouseEvent.MOUSE_DOWN, onClickRussia);
			
			//для чебурашки
			_btnChe.removeEventListener(MouseEvent.ROLL_OVER, onMouseShow);
			_btnChe.removeEventListener(MouseEvent.ROLL_OUT, onMouseHide);
			_btnChe.removeEventListener(MouseEvent.MOUSE_DOWN, onClickBtnChe);
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			if (_image)
			{
				_image.destroy();
				removeChild(_image);
				_image = null;
			}
			
			for each(var i:GlamurBtnBase  in _btns) 
			{
				i.destroy();;
			}
			
			if (_btnChe) 
			{
				removeChild(_btnChe);
				_btnChe = null;
			}
			
			
			onMouseShow(null);
			
			Mouse.show();
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			//trace('data',_data);
			
			updateBtns();
			build();
		};
		
		private function updateBtns():void
		{
			//trace('updateBtns : ' + _btns);
			for each(var i:GlamurBtnBase  in _btns) 
			{
				//trace(i, i.setLang);
				i.setLang(data.lang);
			}
		}
		
		private function onMouseShow(e:MouseEvent):void 
		{
			//trace();
			Mouse.show();
		};
		
		private function onMouseHide(e:MouseEvent):void 
		{
			Mouse.hide();
		};
		
		private function onClickExpo(e:MouseEvent):void 
		{
			////trace('on_ClickExpo');
			removeEvents();
			
			this.mouseChildren = false;
			this.mouseEnabled = false;
			//expo_Btn.anim.visible = true;
			//expo_Btn.anim.gotoAndPlay(1);
			//addEventListener(Event.ENTER_FRAME, onEnterFrameExpo);
			
			Mouse.show();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_RUSSIA;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
			
		};
		
		private function onEnterFrameExpo(e:Event):void 
		{
			if (expo_Btn.anim.currentFrame==expo_Btn.anim.totalFrames) 
			{
				removeEventListener(Event.ENTER_FRAME, onEnterFrameExpo);
				
				//trace('+++');
				
				Mouse.show();
				
				var obj:SectionVO = new SectionVO();
				obj.id = SectionType.PAGE_EXPO_RUSSIA;
				obj.currentId = id;
				obj.params = null;
				
				var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
				ev.data = obj;
				dispatchEvent(ev);
			}
			
		}
		
		private function onClickChildren(e:MouseEvent):void 
		{
			//trace('on_ClickChildren');
			removeEvents();
			
			this.mouseChildren = false;
			this.mouseEnabled = false;
			//child_Btn.anim.visible = true;
			//child_Btn.anim.gotoAndPlay(1);
			//addEventListener(Event.ENTER_FRAME, onEnterFrameDeti);
			
			Mouse.show();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_CHILDREN;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
		};
		
		private function onEnterFrameDeti(e:Event):void 
		{
			//trace('**',child_Btn.anim.currentFrame,child_Btn.anim.totalFrames);
			
			if (child_Btn.anim.currentFrame+1==child_Btn.anim.totalFrames) 
			{
				removeEventListener(Event.ENTER_FRAME, onEnterFrameDeti);
				
				// переходим далее
				
				var obj:SectionVO = new SectionVO();
				obj.id = SectionType.PAGE_CHILDREN;
				obj.currentId = id;
				obj.params = null;
				
				var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
				ev.data = obj;
				dispatchEvent(ev);
			}
		}
		
		
		private function onClickRussia(e:MouseEvent):void 
		{
			//trace('on_ClickChildren');
			removeEvents();
			
			this.mouseChildren = false;
			this.mouseEnabled = false;
			//russia_Btn.anim.visible = true;
			//russia_Btn.anim.gotoAndPlay(1);
			//addEventListener(Event.ENTER_FRAME, onEnterFrameRussia);
			
			Mouse.show();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_RUSSIA_ABOUT;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
		};
		
		private function onEnterFrameRussia(e:Event):void 
		{
			if (russia_Btn.anim.currentFrame==russia_Btn.anim.totalFrames) 
			{
				removeEventListener(Event.ENTER_FRAME, onEnterFrameRussia);
				
				/*// убрать потом как появится контент для этой ветки
				setEvents();
				this.mouseChildren = true;
				this.mouseEnabled = true;
				russia_Btn.anim.gotoAndStop(1);
				russia_Btn.anim.visible = true;*/
				
				// переходим далее
				
				var obj:SectionVO = new SectionVO();
				obj.id = SectionType.PAGE_RUSSIA_ABOUT;
				obj.currentId = id;
				obj.params = null;
				
				var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
				ev.data = obj;
				dispatchEvent(ev);
			}
		}
		
		/**
		 * по клику на чебурашку переходим в карту и маршрут
		 * @param	e
		 */
		private function onClickBtnChe(e:MouseEvent):void 
		{
			removeEvents();
			
			this.mouseChildren = false;
			this.mouseEnabled = false;
			
			Mouse.show();
			
			// переходим далее
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_MAP_AND_ROUTE;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
			
		}
		
	}

}